﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMIS2019
{
    public partial class JobOrder6 : Form
    {
        public string db = "", userName = "", pwd = "", hostname = "";
        public string result = "";

        public static string str;//定义数据库连接字符串
        SqlConnection conn;//定义数据库连接对象

        private void JobOrder6_Load(object sender, EventArgs e)
        {

        }

        private void txtResult_TextChanged(object sender, EventArgs e)
        { }     
        public JobOrder6()
        {
            InitializeComponent();
        }     
                private void btnOpenDB_Click(object sender, EventArgs e)
        {
            db = txtDBName.Text.Trim();
            userName = txtUserName.Text.Trim();
            pwd = txtPassword.Text.Trim();
            hostname = txtHostName.Text.Trim();
            if (hostname.Length == 0)
            {
                MessageBox.Show("请输入服务器名！","系统提示");//消息弹窗
                this.txtHostName.Focus();//光标闪烁
            }
            if (db .Length == 0)
            {
                MessageBox.Show("请输入数据库名称！", "系统提示");//消息弹窗
                this.txtHostName.Focus();//光标闪烁
            }
            if (userName .Length == 0)
            {
                MessageBox.Show("请输入数据库登录用户！", "系统提示");//消息弹窗
                this.txtHostName.Focus();//光标闪烁
            }
            if (pwd .Length == 0)
            {
                MessageBox.Show("请输入数据库用户密码！", "系统提示");//消息弹窗
                this.txtHostName.Focus();//光标闪烁
            }
            if(hostname .Length !=0||db .Length !=0||userName .Length !=0||pwd.Length !=0)
            {
                try
                {
                    //定义数据库连接字符串
                    str = "server=" + hostname + ";User Id=" + userName + ";pwd=" + pwd + ";DataBase=" + db ;
                    //根据数据库连接字符串str实例化数据库连接对象conn
                    conn=new SqlConnection(str);

                    conn.Open();//打开连接
                    if (conn .State ==ConnectionState.Open )
                    {
                        result = "数据库连接成功！";
                        txtResult.Text = result;
                    }
                }
                catch (Exception ex)
                {
                    txtResult.Text  =ex. Message;
                }
            }
        }
    }
  }

